package com.oktest.measure.ui.fragment

import android.graphics.Color
import android.os.Bundle
import com.cali.libcore.base.BaseFragment
import com.cali.libcore.http.RetrofitManagement
import com.oktest.measure.MyApp
import com.oktest.measure.R
import com.oktest.measure.entity.Person
import com.oktest.measure.http.Api
import com.oktest.measure.kt.resultTryContext
import com.oktest.measure.ui.fragment.login.ResetFragment
import com.oktest.measure.ui.fragment.mine.MineSettingFragment
import com.oktest.measure.ui.fragment.work.FeedbackFragment
import kotlinx.android.synthetic.main.fragment_main_mine2.*
import kotlinx.coroutines.launch

/**
 * @Author hhh
 * @Date 2020/9/16
 */
class MineFragment : BaseFragment() {

    override fun getLayoutId(): Int {
        return R.layout.fragment_main_mine2
    }

    override fun initData(savedInstanceState: Bundle?) {
        toolbarHelper?.apply {
            setTitleText("个人信息")
            setTitleTextColor(Color.WHITE)
        }
        superSetting.setOnClickListener {
//            start(FeedbackFragment())
            startParentBrotherFragment(MineSettingFragment())
        }
        superReset.setOnClickListener {
            startParentBrotherFragment(ResetFragment())
        }
        memberInfo()
    }

    private fun memberInfo() = launch {
        val memberResult = resultTryContext {
            RetrofitManagement.instance.getService(Api::class.java)
                .personal(MyApp.memberId).execute().body()
        }
        MyApp.personInfo = memberResult.data?: Person()
        memberResult.data?.apply {
            tvMemberName.text = this.name
            superCompany.setRightString(this.company)
            superPhone.setRightString(this.mobile)
            superSex.setRightString(if(this.sex == "male") "男" else "女")
        }
    }

    override fun hasToolbar(): Boolean {
        return true
    }

    override fun hasBackIcon(): Boolean {
        return false
    }

    companion object {
        fun newInstance(): MineFragment {
            return MineFragment()
        }
    }

    override fun onFragmentResult(requestCode: Int, resultCode: Int, data: Bundle?) {
        super.onFragmentResult(requestCode, resultCode, data)
        findFragment(FeedbackFragment::class.java)?.onFragmentResult(requestCode, resultCode, data)
    }

}